About SharedMemory
version: 1.0.0
last modified: August 28, 2000
SharedMemory is a sample that demonstrates multiple applications using the same memory for IPC under Mac OS X using the Unix shmget() system call.  To test this sample, build it and then launch multiple copies of the built program. The "Key" field is an integer that identifies a shared memory segment.  This can be arbitrary but must be the same in all programs that wish to share that memory.  The "Size" field is the size of the shared memory segment.  If the key is not currently allocated then this field will specify the amount of shared memory to allocate in bytes.  If the key is already allocated and the size requested is more than the actual size of the segment an error will occur, but if it is less than the actual size of the segment then this value is ignored and the program will fill that field with the actual size of the segment.  The "Raw Character Data" field at the bottom represents the current contents of the shared memory segment as a Character string.  Clicking on the "Start Editing" button sets a semaphore to protect the memory and allows you to change the shared memory string.  When you click "Stop Editing" the semaphore is released and copies of the program attached to the same shared memory segment will update their display.  To release the memory simply detach all copies of the program from the shared memory and the last one to detach will attempt to free the memory.
Building the Code
The code was built using a standard Cocoa Application project in project builder.
Restrictions
none.
Other Code Notes
The sample is written in Objective-C using Cocoa;However, the shared memory calls(shmget(),shmat(),shmctl() and shmdt()) are standard System V Unix calls and could be used in a Mach-O Carbon application as well.  If you know C it shouldn't be too difficult to read through the source and comments to pick up how to use the system calls even if you don't know Objective-C.
Known Bugs
none.
Possible Future Enhancements
none.
Other Sources of Reference
the man pages for shmget(), shmat() and shmctl() are probably the first place to look for more detailed descriptions of the calls.  Beyond that try a web search for "shmget", "shmat", "shmctl" or "System V shared memory" which should reveal many links to lecture materials or linux programming resources which can be helpful if you are looking for more examples.  There is also another way of doing shared memory using the BSD mmap() system call which may be of interest but it's more of a hack for sharing page files rather than an API provided for doing Shared memory explicitly like shmget.  Info on using mmap() for shared memory can be found easily with a web search.
Version History
8/16/00-Created
Credits
Karl Groethe
Apple Developer Technical Support